package com.fengfeng.sdk.login;

import android.app.Activity;
import com.fengfeng.http.HttpCallback;
import com.fengfeng.http.HttpUrlConstant;
import com.fengfeng.http.HttpUtils;
import com.fengfeng.sdk.SDKCallback;
import com.fengfeng.sdk.SDKConstants;
import com.fengfeng.sdk.UserCenterManager;
import com.fengfeng.sdk.model.UserInfo;
import com.fengfeng.utils.FFDataUtils;
import com.fengfeng.utils.FFToastUtils;
import com.fengfeng.view.dialog.LoginDialog;
import com.fengfeng.view.dialog.RegisterDialog;
import java.util.HashMap;
import org.json.JSONObject;

/**
 * @author xiaoguokeng
 * @Date 2024/8/7
 */
public class LoginManager {

    private static LoginManager mInstance;
    private SDKCallback mSDKCallback;
    private LoginDialog mLoginDialog;
    private RegisterDialog mRegisterDialog;

    private LoginManager() {
    }

    public static LoginManager getInstance() {
        if (mInstance == null) {
            mInstance = new LoginManager();
        }
        return mInstance;
    }

    public void setCallback(SDKCallback callback) {
        mSDKCallback = callback;
    }

    /**
     * 对外登录方法
     */
    public void login(Activity activity) {
        if (mSDKCallback == null) {
            mSDKCallback.logoutFail(-1, "没有设置监听");
            return;
        }
        UserInfo userInfo = UserCenterManager.getInstance().getUserInfo();
        if (userInfo != null) {
            //如果有用户数据，则自动登录
            loginRequest(userInfo);
        } else {
            //如果没有登录数据，则显示登录弹窗
            mLoginDialog = new LoginDialog(activity, mSDKCallback);
            mLoginDialog.show();
        }
    }

    /**
     * 账号登出方法
     */
    public void logout() {
        if (mSDKCallback == null) {
            mSDKCallback.logoutFail(-1, "没有设置监听");
            return;
        }
        //清空所有数据
        FFDataUtils.saveStringData(SDKConstants.USER_DATA, "");
        mSDKCallback.logoutSuccess();
    }

    /**
     * 注册请求
     */
    public void registerRequest(UserInfo userInfo) {
        //这里就是把账号和密码都放到一个集合里，能理解？这个也不难
        HashMap data = new HashMap<String, Object>();
        data.put("uaccount", userInfo.getAccount());
        data.put("upassword", userInfo.getPassword());
        data.put("pid", "1");
        data.put("gid", "1001");
        HttpUtils.doPost(HttpUrlConstant.REGISTER_URL, data, new HttpCallback() {
            @Override
            public void success(JSONObject data, String msg) {
                //注册成功，就自动登录
                loginRequest(userInfo);
            }

            @Override
            public void error(int code, String msg) {
                FFToastUtils.getInstance().showToast(msg);
            }
        });
    }

    /**
     * 登录请求
     */
    public void loginRequest(UserInfo userInfo) {
        if (mSDKCallback == null) {
            mSDKCallback.logoutFail(-1, "没有设置监听");
            return;
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("uaccount", userInfo.getAccount());
        data.put("upassword", userInfo.getPassword());
        data.put("pid", "1");
        data.put("gid", "1001");
        HttpUtils.doPost(HttpUrlConstant.LOGIN_URL, data, new HttpCallback() {
            @Override
            public void success(JSONObject data, String msg) {
                //登录成功，并获取服务端返回的uid
                String uid = data.optString("uid", "");
                //存储用户数据到SharePreference
                UserInfo newUserInfo = new UserInfo(uid, userInfo.getAccount(), userInfo.getPassword());
                UserCenterManager.getInstance().saveUserData(newUserInfo);
                closeDialog();
                mSDKCallback.loginSuccess(uid);
            }

            @Override
            public void error(int code, String msg) {
                mSDKCallback.loginFail(code, msg);
            }
        });
    }

    /**
     * 展示注册弹窗
     */
    public void showRegister(Activity activity) {
        mRegisterDialog = new RegisterDialog(activity);
        mRegisterDialog.show();
    }

    /**
     * 关闭所有弹窗
     */
    private void closeDialog() {
        if (mLoginDialog != null) {
            mLoginDialog.dismiss();
        }
        if (mRegisterDialog != null) {
            mRegisterDialog.dismiss();
        }
    }

}
